//
//  NestViewController.swift
//  JXPagingView
//
//  Created by jiaxin on 2018/11/2.
//  Copyright © 2018 jiaxin. All rights reserved.
//

import UIKit
import MTCategory
import JXPagingView
import JXCategoryView
import JXSegmentedView
import RxSwift
import RxCocoa
import YPNavigationBarTransition

enum MTFilterPartyTaskType: Int {
    /// 不过滤
    case none = -1
    /// 个人任务
    case single = 3
    ///
    case buss = 4
}

class MTPartyTaskNestViewController: BaseViewController, JXSegmentedViewDelegate {
    var naviDataSource = JXSegmentedTitleDataSource()
    lazy var naviSegmentedView = JXSegmentedView()
    lazy var listContainer = JXSegmentedListContainerView(dataSource: self, type: .scrollView)

    var lists: [JXSegmentedListContainerViewListDelegate?] = []
    
    private lazy var provider: MTProvider<MTClubApi> = {
        let provider = MTProvider<MTClubApi>(plugins: [MTLoadingDataPlugin.init(viewController: self)])
        return provider
    }()
    let userProvider: MTProvider = MTProvider<UserCenterApi>()
    
    lazy var publishTask: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "icon_gift_plus"), for: .normal)
        btn.addTarget(self, action: #selector(create), for: .touchUpInside)
        return btn
    }()
    
    lazy var rightFilterTask: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "condition_black"), for: .normal)
        btn.addTarget(self, action: #selector(fileterTask), for: .touchUpInside)
        return btn
    }()
    
    @objc func fileterTask() {
        // 取消按钮 index = 0
        let clickedHandler = { [weak self] (sheetView: MTActionSheetView, index: Int) in
            guard let weakSelf = self else { return }
            if index == 0 {
                return
            }
            let taskVC: MTJoinTaskViewController = weakSelf.lists.first as! MTJoinTaskViewController
            taskVC.filterPartyTaskData(index == 1 ? MTFilterPartyTaskType.single : index == 3 ? MTFilterPartyTaskType.none : MTFilterPartyTaskType.buss)
        }
        
        let otherButtonTitles = ["个人任务", "商业任务", "全部"]
        let actionSheet = MTActionSheetView(cancelButtonTitle: nil,
                                          otherButtonTitles: otherButtonTitles,
                                          clickedHandler: clickedHandler)
        actionSheet.canTouchToDismiss = true
        actionSheet.show()
        
    }
    
    @objc private func create() {
        self.navigationController?.pushViewController(MTCreatePartyTaskViewController(), animated: true)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let titles = ["任务报名", "我的任务"]
        naviDataSource.titles = titles
        naviDataSource.itemSpacing = 0
        naviDataSource.itemContentWidth = 80
        naviDataSource.titleNormalColor = UIColor.text.x9b9b9b
        naviDataSource.titleSelectedColor = UIColor.text.x1d1a1a
        naviDataSource.isTitleZoomEnabled = true
        naviDataSource.titleNormalFont = PingFangSCSemiboldFont(16)
        naviDataSource.titleSelectedFont = PingFangSCSemiboldFont(16)

        naviSegmentedView.contentEdgeInsetLeft = 15
        naviSegmentedView.contentEdgeInsetRight = 15
        naviSegmentedView.frame = CGRect(x: 0, y: 0, width: 240, height: 30)
        naviSegmentedView.dataSource = naviDataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = UIColor.text.x1d1a1a
        indicator.indicatorWidth = 20
        indicator.indicatorHeight = 3
        naviSegmentedView.indicators = [indicator]
        naviSegmentedView.defaultSelectedIndex = 0
        naviSegmentedView.delegate = self

        navigationItem.titleView = naviSegmentedView

        view.addSubview(listContainer)

        naviSegmentedView.listContainer = listContainer
        
        naviSegmentedView.listContainer?.contentScrollView().panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        
        //navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: publishTask)]
        view.addSubview(publishTask)
        
        publishTask.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-TabBarHeight)
            make.right.equalToSuperview().offset(-10)
            make.size.equalTo(CGSize(width: 48, height: 48))
        }
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: self.rightFilterTask)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        listContainer.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
  
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        self.rightFilterTask.isHidden = index == 0 ? false : true
    }
    
}

extension MTPartyTaskNestViewController: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return 2
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0 {
            if lists.first != nil {
                return lists.first!!
            }
            let mineClub: MTJoinTaskViewController = MTJoinTaskViewController(taskType: .joinTask)
            lists.append(mineClub)
            return mineClub
        }
        
        if lists.count > 1 && lists.last != nil {
            return lists.last!!
        }
        
        let pagingVC = MTPartyTaskNestSubjectViewController()
        pagingVC.nestContentScrollView = listContainer.scrollView
        pagingVC.pagingView.listContainerView.isCategoryNestPagingEnabled = true
        lists.append(pagingVC)
        return pagingVC
    }
}

extension MTPartyTaskNestViewController {
    override func yp_navigtionBarConfiguration() -> YPNavigationBarConfigurations {
        return [.backgroundStyleColor, .backgroundStyleOpaque, .showShadowImage]
    }
}
